package com.desay.pangoo.itmt.repository;

import cn.hutool.core.date.DateTime;
import com.desay.pangoo.itmt.entity.MemberContributionEntity;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author uids1175
 */
@Repository
public interface MemberContributionRepository extends JpaSpecificationExecutor<MemberContributionEntity>,
        PagingAndSortingRepository<MemberContributionEntity, Long> {

    Collection<MemberContributionEntity> findAllByDateBetween(DateTime beginDate,DateTime endDate);

    MemberContributionEntity findTopByOrderByDateDesc();

    MemberContributionEntity findTopByOrderByDateAsc();

    /**
     * 根据nickName获取这个月已经存在成员贡献度记录
     * @param nickNames
     * @return
     */
    @Query("SELECT m FROM MemberContributionEntity m WHERE m.date BETWEEN :startDate AND :endDate " +
            "AND m.nickName IN :nickNames")
    List<MemberContributionEntity> findByDateRangeAndNickNames(
            @Param("startDate") DateTime startDate,
            @Param("endDate") DateTime endDate,
            @Param("nickNames") Set<String> nickNames);

    long countByDate(DateTime dateTime);
}
